package cn.hu.org.service.impl;

import cn.hu.basic.service.impl.BaseServiceImpl;
import cn.hu.org.domain.DictionaryDetail;
import cn.hu.org.domain.DictionaryType;
import cn.hu.org.mapper.DictionaryDetailMapper;
import cn.hu.org.mapper.DictionaryTypeMapper;
import cn.hu.org.service.IDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DictionaryTypeServiceImpl extends BaseServiceImpl<DictionaryType> implements IDictionaryTypeService {

    @Autowired
    private DictionaryTypeMapper dictionaryTypeMapper;
    @Autowired
    private DictionaryDetailMapper dictionaryDetailMapper;

    @Override
    @Transactional
    public void delete(Long id) {
        //通过typeID查到detail有哪些
        DictionaryType typeWithDetails = dictionaryTypeMapper.findOneWithDetails(id);
        List<DictionaryDetail> details = typeWithDetails.getDetails();
        if(details.size()>0){
            //调用detail批量删除
            List<Long> detailids = new ArrayList<>();
            for (DictionaryDetail detail : details) {
                detailids.add(detail.getId());
            }
            dictionaryDetailMapper.patchDelete(detailids);
        }
        //删除type
        dictionaryTypeMapper.delete(id);
    }

    @Override
    public DictionaryType findById(Long id) {
        return dictionaryTypeMapper.findOneWithDetails(id);
    }
}
